//
//  SYRootNaviController.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2018/4/26.
//  Copyright © 2018年 Lssyyy. All rights reserved.
//

#import "SYRootNaviController.h"

@interface SYRootNaviController ()<UINavigationControllerDelegate>

@end

@implementation SYRootNaviController

+ (void)initialize{
    [[UINavigationBar appearance]setBarTintColor:kNaviColor];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        UIButton *backBtn = [self creatBackBtn];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
    }
    [super pushViewController:viewController animated:animated];
}

- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated{
    if (self.viewControllers.count > 1) {
        for (UIViewController *viewController in viewControllers) {
            if (viewController == self.viewControllers.firstObject) {
                continue;
            }
            viewController.hidesBottomBarWhenPushed = YES;
            
            UIButton *backButton = [self creatBackBtn];
            viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        }
    }
    [super setViewControllers:viewControllers animated:animated];
}

- (UIButton *)creatBackBtn{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"IconArrowBack"] forState:0];
    [btn addTarget:self action:@selector(popViewControllerMethod) forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    btn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    
    if (iOS11Later) {
        btn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
        [btn setImageEdgeInsets:UIEdgeInsetsMake(0, -3 *kScreenWidth /375.0,0,0)];
    }
    return btn;
}

- (void)popViewControllerMethod{
    [self popViewControllerAnimated:YES];
}
@end
